﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Fabrikam.Phone.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Fabrikam.Phone.Resources;
using GestureEventArgs = System.Windows.Input.GestureEventArgs;

namespace Fabrikam.Phone
{
    public partial class DetailsPage : PhoneApplicationPage
    {
        // Constructor
        public DetailsPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

       // When page is navigated to set data context to selected item in list
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (DataContext == null)
            {
                string selectedIndex = "";
                if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
                {
                    Guid id = Guid.Parse(selectedIndex);
                    DataContext = App.ViewModel.ServiceTickets.First(model => model.ServiceTicket.ServiceTicketID == id);

                }
            }
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
        private void ApplicationBarDeleteClicked(object sender, EventArgs e)
        {

            ServiceTicketViewModel serviceTicketViewModel = DataContext as ServiceTicketViewModel;

            if (serviceTicketViewModel == null)
                return;

            serviceTicketViewModel.DeleteCommand.Execute(null);
        }

        private void ApplicationBarEditClicked(object sender, EventArgs e)
        {
            ServiceTicketViewModel serviceTicketViewModel = DataContext as ServiceTicketViewModel;

            if (serviceTicketViewModel == null)
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/DetailsEdit.xaml?selectedItem=" + serviceTicketViewModel.ServiceTicket.ServiceTicketID, UriKind.Relative));


        }

        private void OnMapHold(object sender, GestureEventArgs e)
        {
            

        }
    }
}